﻿Imports Kimmo

Public Class frmTables
    Inherits System.Web.UI.Page
    Private strID As String = ""
    Private strAction As String = ""
    Private okScript As String = ""
    Private strView As String = ""
    Private strProjectID As String = ""
    Private strProjectName As String = ""
    Private WithEvents btRefresh As FineUI.Button
    Private WithEvents btNew As FineUI.Button

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.btNew = clsFineUI.ToolBar.AddClickButton(tblLeft, "New", "~/images/newsomething16.gif", "Add a new table")
        Me.btRefresh = clsFineUI.ToolBar.AddClickButton(tblLeft, "Refresh", "~/images/refresh.gif", "Refresh tables for projects")

        strID = WebSite.GetQueryValue("id")
        strAction = WebSite.GetQueryValue("action")
        strView = WebSite.GetQueryValue("view")
        strProjectID = WebSite.GetQueryValue("projectid")
        If Len(strProjectID) > 0 Then clsDL.SaveSetting("projectid", strProjectID)
        If Len(strID) > 0 Then clsDL.SaveSetting("id", strID)
        If Len(strAction) > 0 Then clsDL.SaveSetting("action", strAction)
        If Len(strView) > 0 Then clsDL.SaveSetting("view", strView)
        If IsPostBack Then
        Else
            Select Case strAction
                Case "delete"
                    Tables_DeleteRecords()
            End Select
        End If
        Tables_Report()
    End Sub

    

    Protected Sub Tables_DeletePrompt(ByVal StrID As String)
        clsDL.SaveSetting("deleteid", StrID)
        Me.okScript = "window.location.href='frmTables.aspx?action=delete';"
        clsFineUI.MyConfirmation("Confirm Delete", "Are you sure that you want to delete this table? If you choose Yes, your actions cannot be undone. Delete?", Me.okScript, "")
    End Sub


    Protected Sub Tables_Save(ByRef row As FineUI.GridRow, ByVal strDK As String)
        Dim clsP As New clsTables
        clsP.Read(strDK)
        clsP.TableName = clsDL.TextBoxValue(row, "TableName")
        clsP.TableName = Files.File_CleanName(clsP.TableName)
        clsP.LastUpdated = Today.Date
        clsP.UpdatedBy = ""
        clsP.Save()
        clsColumns.UpdateTableName(clsP.ID.ToString, clsP.TableName)
        clsDL.SaveSetting("view", "tables")
        clsDL.SaveSetting("projectid", clsP.ProjectID)
        Tables_Report()
    End Sub

    Protected Sub Tables_DeleteRecords()
        strID = clsDL.ReadSetting("deleteid")
        If Len(strID) > 0 Then
            strProjectID = clsTables.ProjectIDByID(strID)
            clsTables.DeleteByID(strID)
            clsDL.SaveSetting("projectid", strProjectID)
            clsDL.SaveSetting("view", "tables")
        End If
        WebSite.GotoPage("frmTables.aspx")
    End Sub

    Protected Sub Tables_Report()
        Me.regLeft.Title = "Tables"
        strID = clsDL.ReadSetting("id")
        strView = clsDL.ReadSetting("view")
        strProjectID = clsDL.ReadSetting("projectid")
        Select Case strView
            Case "projects", "project"
                If Len(strProjectID) > 0 Then
                    strProjectName = clsProjects.ProjectNameByID(strProjectID)
                    regLeft.Title = "Tables: " & strProjectName
                    clsTables.ShowTables(grdViews, "projectid = " & strProjectID)
                Else
                    clsTables.ShowTables(grdViews, "")
                End If
            Case "tables"
                If Len(strProjectID) > 0 Then
                    strProjectName = clsProjects.ProjectNameByID(strProjectID)
                    regLeft.Title = "Tables: " & strProjectName
                    clsTables.ShowTables(grdViews, "projectid = " & strProjectID)
                Else
                    clsTables.ShowTables(grdViews, "")
                End If
            Case "table"
                strProjectName = clsTables.ProjectNameByID(strID)
                regLeft.Title = "Tables: " & strProjectName
                clsTables.ShowTables(grdViews, "id = " & strID)
            Case Else
                clsTables.ShowTables(grdViews, "")
        End Select
    End Sub

    Private Sub Tables_Clone(ByVal StrID As String)
        Dim nSV As New clsTables()
        nSV.Read(StrID)
        nSV.ID = CLng(-1)
        nSV.TableName = "Copy of " & nSV.TableName
        nSV.Save()
        clsDL.SaveSetting("view", "table")
        clsDL.SaveSetting("id", nSV.ID.ToString)
        Tables_Report()
    End Sub

    Public Sub grdViews_PageIndexChange(sender As Object, e As FineUI.GridPageEventArgs) Handles grdViews.PageIndexChange
        Me.grdViews.PageIndex = e.NewPageIndex
    End Sub

    Public Sub grdViews_RowCommand(sender As Object, e As FineUI.GridCommandEventArgs) Handles grdViews.RowCommand
        Dim strCommand As String = e.CommandName
        Dim strDK As String = clsFineUI.Grid.RowID(grdViews, e.RowIndex, 0)
        Dim row As FineUI.GridRow = Me.grdViews.Rows(e.RowIndex)
        Select Case strCommand
            Case "save" : Me.Tables_Save(row, strDK)
            Case "clone" : Me.Tables_Clone(strDK)
            Case "delete" : Me.Tables_DeletePrompt(strDK)
            Case "table" : WebSite.GotoPage("frmColumns.aspx?view=columns&tableid=" & strDK)
        End Select
    End Sub

    
    Private Sub btRefresh_Click(sender As Object, e As EventArgs) Handles btRefresh.Click
        clsDL.SaveSetting("view", "tables")
        Tables_Report()
    End Sub

    Private Sub btNew_Click(sender As Object, e As EventArgs) Handles btNew.Click
        Dim strProjectId As String = clsDL.ReadSetting("projectid")
        If Len(strProjectId) > 0 Then
            clsTables.Tables_Blank(strProjectId)
            Tables_Report()
        Else
            clsFineUI.MyAlert("Project Error", "Please select the project to add a table to first!")
        End If
    End Sub
End Class